<?php
class AdminProjectsController extends ModuleAdminController 
{
    public function __construct() 
    {
        $this->table 	  = 'pt_project';
		$this->identifier = 'id_project';
        $this->className  = 'Project';
		
		$this->context = Context::getContext();
		
		$this->addRowAction('view');
		$this->addRowAction('edit');
		$this->addRowAction('delete');
		
		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'), 
				'confirm' => $this->l('Delete selected items?')
			)
		);

		$this->_select = '
            CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
            IF (a.`id_product` = 0, 0, 1) AS `generated`';
		$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'customer` c ON (c.`id_customer` = a.`id_owner`)';
        
        $this->fields_list = array(
			'id_project' => array(
				'title' => $this->l('ID'), 
				'align' => 'center', 
				'width' => 25
			),
            'name' => array(
                'title' 	=> $this->l('Name')
            ),
            'customer' => array(
                'title' 	=> $this->l('Owner'),
            ),
            'id_cms' => array(
                'title' 	=> $this->l('CMS'),
                'callback' 	=> 'getCMSName'
            ),
            'date_add' => array(
                'title' 	=> $this->l('Created On'),
                'type' 	=> 'date'
            ),
            'date_upd' => array(
                'title' 	=> $this->l('Last update On'),
                'type' 	=> 'date'
            ),
    		'is_public' => array(
    			'title' => $this->l('Is Publc'),
    			'width' => 25,
    			'align' => 'center',
    			'type' => 'bool',
    			'tmpTableFilter' => true,
    			'icon' => array(
    				'1' => 'enabled.gif',
    				'0' => 'disabled.gif'
    			),
    			'orderby' => false
    		),
    		'generated' => array(
    			'title' => $this->l('Already Generated'),
    			'width' => 25,
    			'align' => 'center',
    			'type' => 'bool',
    			'tmpTableFilter' => true,
    			'icon' => array(
    				'1' => 'enabled.gif',
    				'0' => 'disabled.gif'
    			),
    			'orderby' => false
    		),
			'active' => array(
				'title' => $this->l('Activated'), 
				'width' => 25, 
				'align' => 'center', 
				'active' => 'status', 
				'type' => 'bool', 
				'orderby' => false
			),
        );

        parent :: __construct();
    }
	
	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('Projects created on prestatheme'),
				'image' => '../img/admin/cog.gif'
			),
			'input' => array(
				array(
					'type' => 'select',
					'label' => $this->l('Project Owner'),
					'name' => 'id_owner',
					'required' => true,
					'options' => array(
						'query' => self::getCustomers(),
						'id' => 'id_customer',
						'name' => 'customer'
					)
				),
				array(
					'type' => 'select',
					'label' => $this->l('Prestatheme CMS'),
					'name' => 'id_cms',
					'required' => true,
					'options' => array(
						'query' => PrestathemeCms::getPrestathemeCMS(),
						'id' => 'id_cms',
						'name' => 'fullname'
					)
				),
				array(
					'type' => 'text',
					'label' => $this->l('Project Name'),
					'name' => 'name',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The name of the project, the name of the futur theme')
				),
				array(
					'type' => 'textarea',
					'label' => $this->l('Description'),
					'name' => 'description',
					'autoload_rte' => true,
					'rows' => 5,
					'cols' => 40,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('About the theme to be generated ...')
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Is Public'),
					'name' => 'is_public',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'is_public_on',
							'value' => 1,
							'label' => $this->l('Yes')
						),
						array(
							'id' => 'is_public_off',
							'value' => 0,
							'label' => $this->l('No')
						)
					),
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Available'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					),
				),
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);

		if (!($obj = $this->loadObject(true)))
			return;

		return parent::renderForm();
	}

	public function renderView()
	{
		if (!($object = $this->loadObject()))
			return;
        
        $adminTabId = Tab::getIdFromClassName('AdminProducts');
		$AdminProductsToken = Tools::getAdminToken('AdminProducts'.(int)$adminTabId.(int)$this->context->employee->id);
        $link = 'index.php?controller=AdminProducts&amp;token='. $AdminProductsToken .'&amp;updateproduct&amp;id_product=';
        			
		$this->tpl_view_vars = array(
			'id_lang' => Context::getContext()->language->id,
			'object' => $object,
			'edit_product_link' => $link,
			'head_title' => ($object->id ? (self::getCMSName($object->id_cms) .' : '. $object->name) : $this->l('New Prestatheme Project')),
			'shopContext' => Shop::getContext(),
		);

		return parent::renderView();
	}
    
    public static function getCustomerName($echo, $row) 
    {
        $id_customer = $row['id_owner'];
        $customer = new Customer($id_customer);
        return $customer->firstname . ' ' . $customer->lastname;
    }
    
    public static function getCustomers ($active = true)
    {
        $sql = 'select CONCAT(LEFT(`firstname`, 1), \'. \', `lastname`, \' &lt;\', `email`, \'&gt;\') AS `customer`, `id_customer` from `' . _DB_PREFIX_ . 'customer` WHERE `deleted` = 0'. ($active ? ' AND `active` = 1' : '');
        if ($rows = Db :: getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) 
        {
            return $rows;
        }
        return array();
    }

    public static function getCMSName ($id_cms)
    {
        $cms = new PrestathemeCms ($id_cms, Context::getContext()->language->id);
        if ($cms)
            return $cms->getCMSName();
        else
            return NULL;
    }
}